package com.whitedust.top80;

import com.whitedust.structure.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author A
 * @since 2025/11/15 23:30
 */

public class Top79 {
    List<Integer> res = new ArrayList<>();

    public boolean isValidBST(TreeNode root) {
        inOrder(root);
        for (int i = 0; i < res.size() - 1; i++) {
            if (res.get(i) > res.get(i + 1)) return false;
        }
        return true;
    }

    private void inOrder(TreeNode root) {
        Stack<TreeNode> st = new Stack<>();
        TreeNode cur = root;
        while (!st.isEmpty() || cur != null) {
            if (cur != null) {
                st.push(cur);
                cur = cur.left;
            } else {
                cur = st.pop();
                res.add(cur.val);
                cur = cur.right;
            }
        }
    }
}
